package com.sxfq.loansupermarket.backsystem.modular.arrival.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.sxfq.loansupermarket.backsystem.core.log.LogManager;
import com.sxfq.loansupermarket.backsystem.core.log.factory.LogTaskFactory;
import com.sxfq.loansupermarket.backsystem.core.shiro.ShiroKit;
import com.sxfq.loansupermarket.backsystem.core.support.HttpKit;
import com.sxfq.loansupermarket.backsystem.core.util.StringUtil;
import com.sxfq.loansupermarket.backsystem.modular.arrival.dao.ReturnVisitDao;
import com.sxfq.loansupermarket.backsystem.modular.arrival.entity.CustomerInfo;
import com.sxfq.loansupermarket.backsystem.modular.arrival.entity.ReturnVisitInfo;
import com.sxfq.loansupermarket.backsystem.modular.arrival.entity.SearchCustomerInfo;
import com.sxfq.loansupermarket.backsystem.modular.arrival.entity.SearchReturnVisitInfo;
import com.sxfq.loansupermarket.backsystem.modular.arrival.service.IReturnVisitService;
import com.sxfq.loansupermarket.backsystem.modular.base.SqlMapper;
import com.sxfq.loansupermarket.backsystem.modular.marketing.entity.ReservationsInfo;
import com.sxfq.loansupermarket.backsystem.modular.marketing.entity.SearchReservationsInfo;
import com.sxfq.loansupermarket.backsystem.modular.system.transfer.UserDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class ReturnVisitServiceImpl implements IReturnVisitService {

    @Autowired
    private ReturnVisitDao dao;

    @Autowired
    private SqlMapper sqlMapper;

    @Override
    public List<ReturnVisitInfo> selectPageListByQueryGetMap(Page<ReturnVisitInfo> page, SearchReturnVisitInfo searchInfo) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("page", page);
        if (StringUtil.isNotEmpty(searchInfo.getOutpatientName())){
            params.put("outpatientName", searchInfo.getOutpatientName());
        }
        if (StringUtil.isNotEmpty(searchInfo.getSreach())) {
            params.put("sreach", searchInfo.getSreach());
        }
        if (StringUtil.isNotEmpty(searchInfo.getCustomerName())) {
            params.put("customerName", searchInfo.getCustomerName());
        }
        if (StringUtil.isNotEmpty(searchInfo.getCustomerPhone())) {
            params.put("customerPhone", searchInfo.getCustomerPhone());
        }
        if (StringUtil.isNotEmpty(searchInfo.getTcName())) {
            params.put("tcName", searchInfo.getTcName());
        }
        if (StringUtil.isNotEmpty(searchInfo.getStartTime())) {
            params.put("startTime", searchInfo.getStartTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getEndTime())) {
            params.put("endTime", searchInfo.getEndTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getFollowStatus())) {
            params.put("followStatus", searchInfo.getFollowStatus());
        }
        if (StringUtil.isNotEmpty(searchInfo.getConsumptionStatus())) {
            params.put("consumptionStatus", searchInfo.getConsumptionStatus());
        }
        params.put("tableName", this.getTableName());
        return dao.getQueryPage(page, params);
    }

    @Override
    public List<ReturnVisitInfo> returnVisitExport(SearchReturnVisitInfo searchInfo) {
        Map<String, Object> params = new HashMap<String, Object>();
        if (StringUtil.isNotEmpty(searchInfo.getOutpatientName())){
            params.put("outpatientName", searchInfo.getOutpatientName());
        }
        if (StringUtil.isNotEmpty(searchInfo.getSreach())) {
            params.put("sreach", searchInfo.getSreach());
        }
        if (StringUtil.isNotEmpty(searchInfo.getCustomerName())) {
            params.put("customerName", searchInfo.getCustomerName());
        }
        if (StringUtil.isNotEmpty(searchInfo.getCustomerPhone())) {
            params.put("customerPhone", searchInfo.getCustomerPhone());
        }
        if (StringUtil.isNotEmpty(searchInfo.getTcName())) {
            params.put("tcName", searchInfo.getTcName());
        }
        if (StringUtil.isNotEmpty(searchInfo.getStartRevisitTime())) {
            params.put("startRevisitTime", searchInfo.getStartRevisitTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getEndRevisitTime())) {
            params.put("endRevisitTime", searchInfo.getEndRevisitTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getFollowStatus())) {
            params.put("followStatus", searchInfo.getFollowStatus());
        }
        if (StringUtil.isNotEmpty(searchInfo.getConsumptionStatus())) {
            params.put("consumptionStatus", searchInfo.getConsumptionStatus());
        }
        if (StringUtil.isNotEmpty(searchInfo.getRevisit())) {
            params.put("revisit", searchInfo.getRevisit());
        }
        params.put("tableName", this.getTableName());
        return dao.getQueryExportPage2(params);
    }

    public List<ReturnVisitInfo> selectPageListByQuery(Page<ReturnVisitInfo> page, SearchReturnVisitInfo searchInfo) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("page", page);
        if (StringUtil.isNotEmpty(searchInfo.getStartTime())) {
            params.put("startTime", searchInfo.getStartTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getEndTime())) {
            params.put("endTime", searchInfo.getEndTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getStartRevisitTime())) {
            params.put("startRevisitTime", searchInfo.getStartRevisitTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getFollower())) {
            params.put("follower", searchInfo.getFollower());
        }
        String listHuifangTime="left(substring_index(a.huifang,',<b>',LENGTH(huifang) - LENGTH(huifang)-1),19)";
        params.put("tableName", this.getTableName());
        params.put("listHuifangTime", listHuifangTime);
        return dao.getQueryPage1(page, params);
    }

    /**
     * tc 回访记录
     * @param page
     * @param searchInfo
     * @return
     */
    public List<ReturnVisitInfo> selectPageConsultantByQuery(Page<ReturnVisitInfo> page, SearchReturnVisitInfo searchInfo) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("page", page);
        if (StringUtil.isNotEmpty(searchInfo.getOutpatientName())){
            params.put("outpatientName", searchInfo.getOutpatientName());
        }
        if (StringUtil.isNotEmpty(searchInfo.getSreach())) {
            params.put("sreach", searchInfo.getSreach());
        }
        if (StringUtil.isNotEmpty(searchInfo.getCustomerName())) {
            params.put("customerName", searchInfo.getCustomerName());
        }
        if (StringUtil.isNotEmpty(searchInfo.getCustomerPhone())) {
            params.put("customerPhone", searchInfo.getCustomerPhone());
        }
        if (StringUtil.isNotEmpty(searchInfo.getTcName())) {
            params.put("tcName", searchInfo.getTcName());
        }
        if (StringUtil.isNotEmpty(searchInfo.getStartRevisitTime())) {
            params.put("startRevisitTime", searchInfo.getStartRevisitTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getEndRevisitTime())) {
            params.put("endRevisitTime", searchInfo.getEndRevisitTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getFollowStatus())) {
            params.put("followStatus", searchInfo.getFollowStatus());
        }
        if (StringUtil.isNotEmpty(searchInfo.getConsumptionStatus())) {
            params.put("consumptionStatus", searchInfo.getConsumptionStatus());
        }
        if (StringUtil.isNotEmpty(searchInfo.getRevisit())) {
            params.put("revisit", searchInfo.getRevisit());
        }
        params.put("tableName", this.getTableName());
        return dao.getQueryPage2(page, params);
    }

    @Override
    public List<ReturnVisitInfo> reservationsExportt(SearchReturnVisitInfo searchInfo) {
        Map<String, Object> params = new HashMap<String, Object>();
        if (StringUtil.isNotEmpty(searchInfo.getFollower())) {
            params.put("follower", searchInfo.getFollower());
        }
        if (StringUtil.isNotEmpty(searchInfo.getStartRevisitTime())) {
            params.put("startRevisitTime", searchInfo.getStartRevisitTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getStartTime())) {
            params.put("startTime", searchInfo.getStartTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getEndTime())) {
            params.put("endTime", searchInfo.getEndTime());
        }
        String listHuifangTime="left(substring_index(a.huifang,',<b>',LENGTH(huifang) - LENGTH(huifang)-1),19)";
        params.put("listHuifangTime", listHuifangTime);
        return dao.getQueryPageExport(params);
    }


    public CustomerInfo selectOneById(String customerId) {
        String sql = "select * from " + this.getTableName() + " where id= '" + customerId + "';";
        CustomerInfo customerInfo = sqlMapper.selectOne(sql, CustomerInfo.class);
        return customerInfo;
    }

    public String getTableName() {
        String tableHead = ShiroKit.getUser().getAbbreviation();
        String tableName = tableHead + "customer_info";
        if (tableHead == "" || tableHead == null) {
            LogManager.me().executeLog(LogTaskFactory.exitLog(ShiroKit.getUser().getId(), HttpKit.getIp()));
            ShiroKit.getSubject().logout();
            return "/login.html";
        }
        return tableName;
    }

}
